package GUILayer;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Point;

import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.JSeparator;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.Time;

import javax.swing.JScrollPane;

import ModelLayer.Employee;

public class TimeListItem extends JPanel 
{
	private JPanel pnlEmpTimes;
	private JLabel lblEmployee;
	private Employee employee;
	private Time selectedTime;
	private JScrollPane scrollPane;
	
	public TimeListItem(Employee employee)
	{
		this.employee = employee;
		setBorder(new LineBorder(Color.LIGHT_GRAY));
		setSize(90, 270);
		setLayout(null);
		
		initCompnents();
		
		addTimesToList();
	}
	
	private void addTimesToList()
	{
		for(String time : employee.getAvailableTimes())
		{
			JLabel lbl = new JLabel(time);
			lbl.setHorizontalAlignment(SwingConstants.CENTER);
			lbl.setCursor(new Cursor(Cursor.HAND_CURSOR));
			lbl.setSize(new Dimension(5, 5));
			
			pnlEmpTimes.add(lbl);
		}
	}
	
	private void initCompnents()
	{
		lblEmployee = new JLabel(employee.getFirstName());
		lblEmployee.setHorizontalAlignment(SwingConstants.CENTER);
		lblEmployee.setBounds(10, 11, 104, 14);
		add(lblEmployee);
		
		JSeparator separator = new JSeparator();
		separator.setBounds(10, 36, 104, 6);
		add(separator);
		
		scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 53, 104, 206);
		add(scrollPane);
		
		pnlEmpTimes = new JPanel();
		scrollPane.setViewportView(pnlEmpTimes);
		pnlEmpTimes.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				updateSelected(arg0);
			}
		});
		pnlEmpTimes.setLayout(new GridLayout(employee.getAvailableTimes().size(), 1, 0, 0));
		
		selectedTime = null;
	}
	
	private void updateSelected(MouseEvent arg0)
	{
		Point point = arg0.getPoint();
		
		JLabel label = (JLabel)pnlEmpTimes.getComponentAt(point);
		
		if(label != null)
		{
			selectedTime = Time.valueOf(label.getText());
			TimeList timeList = (TimeList)this.getParent().getParent();
			timeList.setSelectedItem(this);
		}
	}
	
	public Employee getEmployee()
	{
		return employee;
	}
	
	public Time getSelectedTime()
	{
		return selectedTime;
	}
}
